package com.foreknow.controller;

import com.foreknow.model.Guestbook;
import com.foreknow.service.GuestbookService;
import com.foreknow.service.impl.GuestbookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

public class FirstServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        //设置服务器端向客户端响应的内容的类型(MIME) https://www.runoob.com/http/mime-types.html
//        resp.setContentType("text/html;charset=utf-8");
//        //设置请求的字符编码集
//        req.setCharacterEncoding("utf-8");
        //获取输出流主要用于向网页中显示信息
        PrintWriter out = resp.getWriter();
        //如何获取请求参数 ?username=fender&password=123456
        String username = req.getParameter("username"); //username要与表单元素中的name属性值相同
        String password = req.getParameter("password");
        //调用Service中的login方法
        GuestbookService service = new GuestbookServiceImpl();
        Guestbook guestbook = service.login(username,password);
        if(guestbook != null) {
            //在网页中输出结果
            //out.println(guestbook.getName() + "---" + guestbook.getPass());
            //如果登录成功就要转发请求
            req.getRequestDispatcher("manager?q=list").forward(req,resp);
        }else {
            out.println("error......");
        }


    }
}
